﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;

namespace HeritageAcademy.Library.Controls
{
    public class SmartColumnsPanel : Panel
    {
		#region Fields 

        double columnWidth = 0;
        Size totalSize;

		#endregion Fields 

		#region Protected Methods 

        protected override Size ArrangeOverride(Size finalSize)
        {
            int columnCount = 1;

            double left = 0;
            double top = 0;

            foreach (UIElement child in this.Children)
            {
                if (top > 0 && (top + child.DesiredSize.Height > finalSize.Height))
                {
                    top = 0;
                    left += columnWidth;
                    columnCount++;
                }

                child.Arrange(new Rect(left, top, columnWidth, child.DesiredSize.Height));
                top += child.DesiredSize.Height;

            }

            totalSize = new Size(columnWidth * columnCount, finalSize.Height);

            return totalSize;
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            // Avoid exception when availableSize.Height is Infinity
            availableSize.Height = Math.Min(SystemParameters.FullPrimaryScreenHeight, availableSize.Height);

            columnWidth = this.GetColumnWidth();
            double top = 0;
            int columnCount = 1;

            foreach (UIElement child in this.Children)
            {
                child.Measure(new Size(columnWidth, availableSize.Height));

                if (top > 0 && (top + child.DesiredSize.Height > availableSize.Height))
                {
                    top = 0;
                    columnCount++;
                }

                top = top + child.DesiredSize.Height;
            }

            totalSize = new Size(columnWidth * columnCount, availableSize.Height);
            return totalSize;
        }

		#endregion Protected Methods 

		#region Private Methods 

        double GetColumnWidth()
        {
            double count = Math.Max(Math.Floor(this.AvailableWidth / this.MinimumColumnWidth), 1);
            double width = this.AvailableWidth / count;
            return width;
        }

		#endregion Private Methods 

        #region MinimumColumnWidth

        public static readonly DependencyProperty MinimumColumnWidthProperty =
            DependencyProperty.Register("MinimumColumnWidth", typeof(double), typeof(SmartColumnsPanel),
                new FrameworkPropertyMetadata((double)325,
                    FrameworkPropertyMetadataOptions.AffectsMeasure));

        public double MinimumColumnWidth
        {
            get { return (double)GetValue(MinimumColumnWidthProperty); }
            set { SetValue(MinimumColumnWidthProperty, value); }
        }

        #endregion

        #region AvailableWidth

        public static readonly DependencyProperty AvailableWidthProperty =
            DependencyProperty.Register("AvailableWidth", typeof(double), typeof(SmartColumnsPanel),
                new FrameworkPropertyMetadata((double)0.0,
                    FrameworkPropertyMetadataOptions.AffectsMeasure));

        public double AvailableWidth
        {
            get { return (double)GetValue(AvailableWidthProperty); }
            set { SetValue(AvailableWidthProperty, value); }
        }

        #endregion
    }
}
